//
//  FeedBackFormView.m
//  FeedBackMod
//
//  Created by sischen on 16/5/14.
//  Copyright (c) 2016年 neo6. All rights reserved.
//

#import "FeedBackFormView.h"


@interface FeedBackFormView () <UITextViewDelegate>
{
	CGFloat _prompt_H;//!<prompt提示文字高度
	NSMutableArray *_selViewArr;//!<选项viewArray
}

//异常备注
@property(nonatomic, strong)NSString *exceptionState;
@property(nonatomic, strong)NSString *exceptionRemark;
@end

@implementation FeedBackFormView


-(id)initWithFrame:(CGRect)frame andForm:(FeedBackForm *)form
{
	if (self = [super initWithFrame:frame]) {
		
		self.form = form;
		_selViewArr = [NSMutableArray array];
		_prompt_H = form.prompt == nil ? 0 : SEL_H;
		
		
		if ([self.form.type isEqualToString:SIG_SEL] || [self.form.type isEqualToString:MUL_SEL]) {
			
			NSInteger selcount = self.form.selectionsArray.count;
			self.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, _prompt_H + SEL_H * selcount);
			
		}else if ([self.form.type isEqualToString:TXT]){
			
			self.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, _prompt_H + TEXT_H);
		}
	}
	return self;
}

-(void)disPlayForm
{
	
	if (self.form.prompt) {
		self.promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, self.bounds.size.width, _prompt_H)];
		self.promptLabel.text = [NSString stringWithFormat:@"  %@",self.form.prompt];
		self.promptLabel.font = [UIFont systemFontOfSize:16];
		self.promptLabel.textAlignment = NSTextAlignmentLeft;
//        self.promptLabel.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.1];
        self.promptLabel.backgroundColor=[UIColor clearColor];
		[self addSubview:self.promptLabel];
	}

	
	if ([self.form.type isEqualToString:SIG_SEL] || [self.form.type isEqualToString:MUL_SEL]) {
		
		NSInteger selcount = self.form.selectionsArray.count;
		for (NSInteger i = 0; i < selcount; i++) {
			
			UICheckView *checkview = [[UICheckView alloc] initWithFrame:CGRectMake(0, _prompt_H+i*SEL_H, self.bounds.size.width, SEL_H) type:[self.form.type isEqual:SIG_SEL] ? UICheckViewTypeSingle : UICheckViewTypeMulti checked:NO];
			FeedBackSelection *sel = self.form.selectionsArray[i];
			[checkview setText:sel.select_desc];
			[checkview setSel_id:sel.select_id];
			
			checkview.stateChangedBlock = ^void(UICheckView *cv){
				[self clearOtherSelWhenSingle:cv];
			};
			
			[_selViewArr addObject:checkview];
			[self addSubview:checkview];
		}
		
	}else if ([self.form.type isEqualToString:TXT]){
        float a=self.bounds.size.height;
        float b=self.bounds.size.height-_prompt_H-SEL_H/2;
		self.textview = [[UITextView alloc] initWithFrame:CGRectMake(SEL_H/4, SEL_H/4+_prompt_H, self.bounds.size.width-SEL_H/2, self.bounds.size.height-_prompt_H-SEL_H/2)];
        [self.textview setTag:3];

		self.textview.delegate = self;
		
		self.textview.text = self.form.hint;
		self.textview.textColor = [UIColor colorWithWhite:0.6 alpha:0.6];
		self.textview.font = [UIFont systemFontOfSize:15];
		self.textview.layer.cornerRadius = 5;
		self.textview.layer.borderWidth = 0.5;
		self.textview.layer.borderColor = [UIColor colorWithWhite:0.3 alpha:0.3].CGColor;
		self.textview.clipsToBounds = YES;
		[self addSubview:self.textview];
        
        if ([self.form.tag isEqualToString:@"二维码"]) {
            self.textview.editable=NO;
        UIButton*okButton=[[UIButton alloc] init];
        [okButton setBackgroundColor:[UIColor clearColor]];
            [okButton addTarget:self action:@selector(check) forControlEvents:UIControlEventTouchUpInside];
//        okButton.imageEdgeInsets = UIEdgeInsetsMake(10, (SCREEN_WIDTH- 10)/2, 10, (SCREEN_WIDTH- 10)/2);
        [self.textview addSubview:okButton];
            self.okButton=okButton;
            
        [okButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self.textview);
            make.width.height.mas_equalTo(@30);
        }];
        [okButton setImage:[UIImage imageNamed:@"icon_checkbox_empty"] forState:UIControlStateNormal];
        [self.okButton setImage:[UIImage imageNamed:@"icon_checkbox_selected"] forState:UIControlStateSelected];
            if (![MySingleton sharedMySingleton].exceptionState)
                self.okButton.selected=YES;
            //记住异常备注状态
            if ([_line.exceptionState isEqualToString:@"1"]) {
//                [self.okButton setImage:[UIImage imageNamed:@"icon_checkbox_selected"] forState:UIControlStateNormal];
                self.okButton.selected=YES;

            }
            //记住异常备注状态
            if ([_line.exceptionState isEqualToString:@"0"]) {
                [self.okButton setSelected:NO];
            }

        }
        if ([self.form.tag isEqualToString:@"二维码err"])
        {
            if (_line.exceptionRemark) {
                self.textview.text=_line.exceptionRemark;
            }
//            if ([MySingleton sharedMySingleton].finished) {
//                if ([MySingleton sharedMySingleton].exceptionRemark) {
//                    self.textview.text=[MySingleton sharedMySingleton].exceptionRemark;
//
//                }
//            }
        }
        if ([self.form.tag isEqualToString:@"拍摄照片"]) {
            self.textview.editable=NO;
            self.textview.text=@"";
            UIButton*okButton=[[UIButton alloc] init];
            [okButton setBackgroundColor:[UIColor clearColor]];
            [okButton addTarget:self action:@selector(picture) forControlEvents:UIControlEventTouchUpInside];
            //        okButton.imageEdgeInsets = UIEdgeInsetsMake(10, (SCREEN_WIDTH- 10)/2, 10, (SCREEN_WIDTH- 10)/2);
            [self.textview addSubview:okButton];
            self.okButton=okButton;
            
            [okButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.mas_equalTo(self.textview);
                make.height.mas_equalTo(@28);
                make.width.mas_equalTo(@32);
            }];
            [okButton setImage:[UIImage imageNamed:@"ic_photo_camera"] forState:UIControlStateNormal];
            [self.okButton setImage:[UIImage imageNamed:@"icon_checkbox_selected"] forState:UIControlStateSelected];
            if ([MySingleton sharedMySingleton].finished)
            {
                [self.okButton setImage:[UIImage imageNamed:@"ic_photo_camera_withimage"] forState:UIControlStateNormal];
            }
            if (_model.picture) {
                [self.okButton setImage:[UIImage imageNamed:@"ic_photo_camera_withimage"] forState:UIControlStateNormal];
            }
            if ([MySingleton sharedMySingleton].errPic) {
                [self.okButton setImage:[UIImage imageNamed:@"ic_photo_camera_withimage"] forState:UIControlStateNormal];
            }
        }
        if ([self.form.tag isEqualToString:@"整体审查"]) {
            self.textview.editable=NO;
            UILabel*okButton=[[UILabel alloc] init];
            [okButton setBackgroundColor:[UIColor clearColor]];
            [okButton setText:[MySingleton sharedMySingleton].finalscore];
            okButton.font = [UIFont xm_medium:20.0];
            okButton.numberOfLines = 1;
            okButton.textColor = [UIColor xm_jetblackColor];
            okButton.textAlignment=UITextAlignmentCenter;
//            [okButton addTarget:self action:@selector(check) forControlEvents:UIControlEventTouchUpInside];
            //        okButton.imageEdgeInsets = UIEdgeInsetsMake(10, (SCREEN_WIDTH- 10)/2, 10, (SCREEN_WIDTH- 10)/2);
            [self.textview addSubview:okButton];
            
            [okButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.mas_equalTo(self.textview);
                make.width.mas_equalTo(@100);
            }];
        }
        if ([self.form.tag isEqualToString:@"整体审查1"]) {
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            NSString* password = [defaults objectForKey:@"audit"];
//            if (password) {
//                self.textview.text=password;
//            }
            if (_workModel.auditUser) {
                self.textview.text=_workModel.auditUser;
            }
        }
        if ([self.form.tag isEqualToString:@"整体审查2"]) {
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            NSString* password = [defaults objectForKey:@"auditRemark"];
            if (password) {
                self.textview.text=password;
            }
        }
        if ([self.form.tag isEqualToString:@"注意事项"]) {
            self.textview.editable=NO;
            self.textview.frame=CGRectMake(SEL_H/4, SEL_H/4+_prompt_H, self.bounds.size.width-SEL_H/2, self.bounds.size.height);
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            NSString* password = [defaults objectForKey:@"guideline"];
            if (password)
                self.textview.text=password;
            [self.textview setTextColor:[UIColor blackColor]];
//            self.textview.layer.borderColor = [UIColor clearColor].CGColor;
            

        }
        if ([self.form.tag isEqualToString:@"NOTOK"]) {
            if ([MySingleton sharedMySingleton].finished)
            {
                if (_model) {
                    self.textview.text=_model.remark;
                }
            }
            if (_model.remark) {
                self.textview.text=_model.remark;
            }
        }
        

	}
}
-(void)check{
    self.okButton.selected=!self.okButton.selected;
    if (self.okButton.selected) {
        [MySingleton sharedMySingleton].exceptionState=@"1";
    }
    else{
        [MySingleton sharedMySingleton].exceptionState=@"0";
    }
    if (self.blockError) {
        self.blockError([MySingleton sharedMySingleton].exceptionState, @"");
    }
}
-(void)picture{
    if (self.block) {
        self.block();
    }
}
/**单选时，清除其他已选项
 */
- (void)clearOtherSelWhenSingle:(UICheckView *)cv
{
	for (UICheckView *chv in _selViewArr) {
		if (chv.sel_id != cv.sel_id && [self.form.type isEqualToString:SIG_SEL]) {
			chv.checked = NO;
		}
	}
}

/**实现place holder效果,开始编辑时,清除hint
 */
-(void)textViewDidBeginEditing:(UITextView *)textView
{
//	NSLog(@"开始编辑");
	if ([textView.text isEqualToString:self.form.hint]) {
		textView.text = @"";
	}
	textView.textColor = [UIColor blackColor];
	
	self.keyboardBlock(textView);
}

/**实现place holder效果,结束编辑时,如未输入,恢复显示hint
 */
-(void)textViewDidEndEditing:(UITextView *)textView
{
//	NSLog(@"结束编辑");
	
	if (textView.text.length == 0) {
		textView.text = self.form.hint;
		textView.textColor = [UIColor colorWithWhite:0.6 alpha:0.6];
	}
    
    if (self.blockError) {
        if (![MySingleton sharedMySingleton].exceptionState) {
            [MySingleton sharedMySingleton].exceptionState=@"1";
        }
        self.blockError([MySingleton sharedMySingleton].exceptionState, textView.text);
    }
    if (self.blockComment) {
        self.blockComment(textView.text);
    }
    if ([self.form.prompt isEqualToString:@"2.审计人员姓名"]) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *token = @"audit";
        [defaults setObject:textView.text forKey:token];
        [defaults synchronize];
        if (self.blockUser) {
            self.blockUser(textView.text);
        }
    }
    if ([self.form.prompt isEqualToString:@"3.备注"]) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *token = @"auditRemark";
        [defaults setObject:textView.text forKey:token];
        [defaults synchronize];
    }
}

/**当点击回车时,结束输入,键盘收起
 */
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
	if ([text isEqualToString:@"\n"]){
		[textView resignFirstResponder];
		return NO;
	}
    if (self.blockComment) {
        self.blockComment(textView.text);
    }
	return YES;
}


@end
